const { Sequelize } = require("sequelize");
const seq = require("./seq");

// 创建User模型。数据表的名字是users
const User = seq.define("user", {
  userName: {
    type: Sequelize.STRING,
    allowNull: false
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false
  },
  nickName: {
    type: Sequelize.STRING,
    comment: "昵称"
  }
});

const Blog = seq.define("blog", {
  title: {
    type: Sequelize.STRING,
    allowNull: false
  },
  content: {
    type: Sequelize.TEXT,
    allowNull: false
  },
  userId: {
    type: Sequelize.INTEGER,
    allowNull: false
  }
});

// 创建外键关联
Blog.belongsTo(User, {
  // Blog.userId ->User.id
  foreignKey: "userId"
});
// 简便写法
// Blog.belongsTo(User);

// 另一种写法，两个都写跟查询的顺序有关
User.hasMany(Blog, {
  foreignKey: "userId"
});

module.exports = {
  User,
  Blog
};
